window.onload = function() {
    // var youxiangRegister = document.getElementsByClassName("youxiangRegister ")[0];
    var registerr = document.getElementsByClassName("registerr")[0]

    registerr.onmouseover = function() {
        this.style.color = "orange"
    }
    registerr.onmouseout = function() {
            this.style.color = "#fff"
        }
        //邮箱注册之邮箱验证码
    var youxiangZhuce = document.getElementsByClassName("youxiangZhuce")[0];
    var removes = document.getElementById("removes");
    var registerr = document.getElementsByClassName("registerr")[0];
    var youxiangleft = document.getElementById('youxiangleft')
    var youxianggo = document.getElementById('youxianggo')
    var emailregister = document.getElementById('emailregister')
    var getemail = document.getElementById('getemail')
    var youxiangZhuces = document.getElementsByClassName('youxiangZhuces')[0]

    registerr.onclick = function() {
        youxiangZhuce.style.display = "block"

    }
    removes.onclick = function() {
        youxiangZhuce.style.display = "none"
    }
    getemail.onclick = function() {
            axios({
                method: 'GET',
                url: '/register/email',
                params: {
                    email: emailregister.value
                }
            }).then(data => {
                console.log(data.data.data);

                if (!emailregister.value) {
                    alert('邮箱不能为空！')
                } else if (data.data.data.message === '成功') {
                    alert('邮箱验证码已发送！')
                } else if (data.data.data.message === '该邮箱已被注册!') {
                    alert('该邮箱已被注册！')
                } else if (data.data.data.message === '错误的邮箱格式') {
                    alert('邮箱格式错误！')
                } else {
                    alert('邮箱验证码发送失败！')

                }
            }).catch(err => {
                alert('no')
                console.log(err);
            })
        }
        //邮箱注册之验证验证码是否正确
    youxianggo.onclick = function() {
            axios({
                method: 'POST',
                url: '/rightcode',
                params: {
                    email: emailregister.value,
                    code: youxiangleft.value
                }
            }).then(data => {
                alert('邮箱验证码正确！')
                youxiangZhuces.style.display = 'block'
                    //终极邮箱注册
                var removesa = document.getElementById('removesa')
                var registerPasswords = document.getElementById('registerPasswords')
                var trueregister = document.getElementById('trueregister')
                removesa.onclick = function() {
                    youxiangZhuces.style.display = 'none'
                }

                trueregister.onclick = function() {

                    axios({
                        method: 'POST',
                        url: '/finalregister',
                        params: {
                            email: emailregister.value,
                            password: registerPasswords.value
                        }
                    }).then(data => {

                        if (!emailregister.value || !registerPasswords.value) {
                            alert('请输入邮箱或密码!')
                        } else if (data.data.data.message === '请输入正确的密码') {
                            alert('请输入正确的密码格式！')
                        } else if (data.data.data.message === '成功') {
                            alert('邮箱注册成功！')
                        }
                    }).catch(err => {
                        console.log(err);
                        alert('no')
                    })
                }
            }).catch(err => {
                console.log(err);
                alert('邮箱验证码错误!')
            })

        }
        //注册
    var registerName = document.getElementById('registerName')
    var registerPassword = document.getElementById('registerPassword')
    var registerCode = document.getElementById('registerCode')
    var givein = document.getElementById('givein')
    givein.onclick = function() {
        axios({
            method: 'POST',
            url: '/user/register',
            params: {
                zhanghao: registerName.value,
                mima: registerPassword.value,
                code: registerCode.value
            }
        }).then(data => {

            if (!registerName.value || !registerPassword.value) {
                alert('请输入账号或密码！')
            } else if (data.data.data.message === '请输入正确的验证码') {
                alert('请输入正确的验证码！')
            } else if (data.data.data.message === '请输入正确的账号') {
                alert('请输入正确的账号格式！')
            } else if (data.data.data.message === '验证码错误') {
                alert('请输入正确的验证码！ ')
            } else if (data.data.data.message === '成功') {
                alert('注册成功！')
            } else if (data.data.data.message === '该账号已经被注册') {
                alert('该账号已经被注册!')
            } else {
                alert('注册失败！')
            }


        }).catch(err => {

            console.log(err);
            alert('no')
        })
    }

    // //验证码更新
    // let srcCode = document.querySelector('.srcCode')
    // srcCode.addEventListener('click', () => {
    //     SrcCod()
    // })

    // function SrcCod() {
    //     srcCode.src = `http://101.35.138.144:8080/image/Kaptcha/defaultKaptcha?d=${new Date()*1}`
    // }

    //验证码更新(邮箱登录)
    let sss = document.getElementsByClassName('sss')[0]
    sss.onclick = function() {
        SrcCoddd()
    }

    function SrcCoddd() {
        axios.get('/user/code').then((data) => {
            let message = data.data.data.img
            sss.setAttribute('src', message);
        })
    }
    SrcCoddd()
}